package com.hhang.boot.controller;

import com.github.pagehelper.PageInfo;
import com.hhang.boot.exception.ServiceException;
import com.hhang.boot.service.ComService;
import com.hhang.boot.utils.ResponseDataBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/com")
public class ComController {


    @Autowired
    private ComService comService;

    //goods部分
    @PostMapping("/addGoods")
    public Map<String,Object> addGoods(@RequestBody Map<String,Object> param){
        try {
            int res = comService.addGoods(param);
            return new ResponseDataBuilder().success().message("添加成功").data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c服务异常").build();
        }
    }

    @GetMapping("/findCom")
    public Map<String,Object> findCom(@RequestParam Map<String,Object> param){
        try {
            PageInfo<Map<String,Object>> byName = comService.findCom(param);
            return new ResponseDataBuilder().success().data(byName).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }

    @PostMapping("/upGoods")
    public Map<String,Object> upGoods(@RequestBody Map<String,Object> param){
        try {
            int res = comService.upGoods(param);
            return new ResponseDataBuilder().data(res).success().build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c更新失败").build();
        }
    }

    @DeleteMapping("/delGoods")
    public Map<String,Object> delGoods(@RequestParam (value = "ids") Long[] ids){
        try {
            int res = comService.delGoods(Arrays.asList(ids));
            return new ResponseDataBuilder().success().data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }
    //  报修表部分

    @GetMapping("/findRepair")
    public Map<String,Object> findRepair(@RequestParam Map<String,Object> param){
        try {
            PageInfo<Map<String,Object>> petsList = comService.findRepair(param);
            return new ResponseDataBuilder().success().data(petsList).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }

    //  排班部分
    @PostMapping("/addDuty")
    public Map<String,Object> addDuty(@RequestBody Map<String,Object> param){
        try {
            int res = comService.addDuty(param);
            return new ResponseDataBuilder().success().message("添加成功").data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c服务异常").build();
        }
    }

    @GetMapping("/findDuty")
    public Map<String,Object> findDuty(@RequestParam Map<String,Object> param){
        try {
            PageInfo<Map<String,Object>> dutyList = comService.findDuty(param);
            return new ResponseDataBuilder().success().data(dutyList).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("查询失败_C").build();
        }
    }

    @DeleteMapping("/delDuty")
    public Map<String,Object> delDuty(@RequestParam (value = "ids") Long[] ids){
        try {
            int res = comService.delDuty(Arrays.asList(ids));
            return new ResponseDataBuilder().success().data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }

    @PostMapping("/addRepair")
    public Map<String,Object> addRepair(@RequestBody Map<String,Object> param){
        try {
            int res = comService.addRepair(param);
            return new ResponseDataBuilder().success().message("申请成功").data(res).build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message(e.getMessage()).build();
        }
    }



    @PostMapping("/upRepair")
    public Map<String,Object> upRepair(@RequestBody Map<String,Object> param){
        try {
            int res = comService.upRepair(param);
            return new ResponseDataBuilder().data(res).success().build();
        } catch (ServiceException e) {
            e.printStackTrace();
            return new ResponseDataBuilder().error().message("error_c更新失败").build();
        }
    }
}
